import api from '../../../utils/api'
// import uuid from '../../utils/modules/uuid'
// import util from '../../../utils/util

Page({
    data: {
        list: [],
        page: 0,
        title: '气色观察',
        loading: false,
        positionId: "",
        uuid: "",
        type: "",
        shareTitle: ""
    },
    onLoad (options) {
        const { examid, token } = options
        if(examid&&token) {
             this.loginToMirror(examid)
        }
        const title = '气色观察'    
        // let positionId = options.positionId
        // let uuid = options.uuid
        // let type = options.type
        // let shareTitle = options.title
        // this.setData({
        //     title,
        //     positionId,
        //     uuid,
        //     type,
        //     shareTitle
        // })
        // wx.setNavigationBarTitle({ title })
        // wx.showLoading({
        //     title: '加载中'
        // })
        // this.getData()
        this.gethistrorylist()
    },
    loginToMirror(examid) {
        let data = {
            examid
        }
          let url = `https://mirror.laikang.com/api/mirror/exam/updateExamUserid`
    
        api.adminPost(url, { data }).then(res => {
          console.log('请求带走成功:', res.data);
        //   Toast('扫码登录成功');
        }).catch(e => {
            console.log(e)
            // Toast('请登录');
        })
      },
    handlerTo (e) {
        console.log(e)
        const { url, zheng } = e.currentTarget.dataset

        // if (!zheng) {
        //     wx.showModal({
        //         title: '提示',
        //         content: '报告预览功能正在开发',
        //         showCancel: false
        //     })
        //     return
        // }
        wx.navigateTo({
            url,
        })
    },
    handlerTojia() {
        wx.navigateTo({
            url: `/facetongue/pages/jia/index`
        })
    },
    getDeFen: text => {
        try {
            if (text.includes('得分')) {
                return Number(text.split('，')[0].split('：')[1])
            } else {
                return null
            }
        } catch (e) {
            return null
        }
    },
    gethistrorylist() {
		api
			.post('/api/hxhealth/exams/records', {
				data: {
                    everyday: true,
                    devkind: 'facediag,tonguediag',
					everyday: false
				}
			})
			.then((res) => {
				// console.log(res);
                console.log('----历史数据----', res);
                const list = res.map(item => {
                    if (this.data.title === '测肤记录') {
                        item.defen = Number(JSON.parse(item.exam_otherresult).multipleScore)
                    } else {
                        console.log(JSON.parse(item.summary));
                        item.defen = this.getDeFen(JSON.parse(item.summary).evaluation)
                        item.examid = JSON.parse(item.summary).laikangExamId
                        item.laikangExamToken = JSON.parse(item.summary).laikangExamToken
                    }
                    item.tizheng = JSON.parse(item.summary).diseaseName
    
                    // try {
                    //     item.fuzhi = JSON.parse(item.exam_otherresult).skinTexture
                    // } catch (e) {
                    //     item.fuzhi = ''
                    // }
                    // item.timeA = item.exam_time.split(' ')[0]
                    // item.timeB = item.exam_time.split(' ')[1]
                    return item
                })
                this.setData({
                    list,
                    loading: false
                })
                
                console.log('====================================');
                console.log(list);
                console.log('====================================');
			});
	},
    getData () {
        this.setData({
            loading: true
        })
        let url = '/api/hxhealth/exams/records'
        let data = {
            offset: this.data.page,
            limit: 100,
            sort: 'DESC',
            // examSource: "mirror",
            examSpace: 'PD-FUZHEN',
            isEvaluation: 0
        }
        if (this.data.title === '气色观察') {
            data.examSpace = 'FACE_TONGUE,PD,FACE_TONGUE_LK'
            util.historyPage.set({ url: 'package-mirror/qsgc-index/index' })
        }

        api.adminPost(url, { data }).then(res => {
            console.log('记录列表', res)

            const list = res.map(item => {
                if (this.data.title === '测肤记录') {
                    item.defen = Number(JSON.parse(item.summary).multipleScore)
                } else {
                    console.log(JSON.parse(item.summary));
                    item.defen = util.getDeFen(item.exam_evaluation)
                }
                item.tizheng = formatTz(item.exam_conclusion || item.exam_evaluation || item.exam_alladvice, item.exam_sourcekind)

                try {
                    item.fuzhi = JSON.parse(item.exam_otherresult).skinTexture
                } catch (e) {
                    item.fuzhi = ''
                }
                item.timeA = item.exam_time.split(' ')[0]
                item.timeB = item.exam_time.split(' ')[1]
                return item
            })
            this.setData({
                list,
                loading: false
            })

            wx.hideLoading()
            wx.stopPullDownRefresh()
        }).catch(e => {
            console.log(e)
            this.setData({
                loading: false
            })
            wx.stopPullDownRefresh()
            wx.hideLoading()
        })
    },
    handlerNextPageFn () {
        if (this.data.loading && this.data.list.length > 0) {
            console.log('正在获取中，无法重复获取')
            return
        }
        let page = this.data.page + 1
        this.data.page = page
        this.getData()
    },

    onShow: function () {

    },
    onHide: function () {

    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {
        this.data.page = 0
        this.getData()
    },
    onReachBottom: function () {

    },
})
